<template>
  <a-modal
    title="编辑"
    :width="900"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @ok="handleSubmit"
    @cancel="handleCancel"
  >
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">
        <a-form-item v-show="false"><a-input v-decorator="['id']" /></a-form-item>
        <a-form-item
          label="权重"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          has-feedback
        >
          <a-input placeholder="请输入权重1-100数字" v-decorator="['weight', {rules: [{required: true, message: '请输入权重1-100！'}]}]" />
        </a-form-item>
        <a-form-item
          label="商户名称"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          has-feedback
        >
          <a-input placeholder="请输入商户名称" v-decorator="['mchName', {rules: [{required: true, message: '请输入商户名称！'}]}]" />
        </a-form-item>
        <a-form-item
          label="AppId"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          has-feedback
        >
          <a-input placeholder="请输入AppId" v-decorator="['appId', {rules: [{required: true, message: '请输入AppId！'}]}]" />
        </a-form-item>
        <a-form-item
          label="直连商户id"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          has-feedback
        >
          <a-input placeholder="请输入直连商户id" v-decorator="['mchId', {rules: [{required: true, message: '请输入直连商户id！'}]}]" />
        </a-form-item>
        <a-form-item
          label="商户api证书序列号"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          has-feedback
        >
          <a-input placeholder="请输入商户api证书序列号" v-decorator="['mchSerialNo', {rules: [{required: true, message: '请输入商户api证书序列号！'}]}]" />
        </a-form-item>
        <a-form-item
          label="V3密钥"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          has-feedback
        >
          <a-input placeholder="请输入V3密钥" v-decorator="['appKey', {rules: [{required: true, message: '请输入V3密钥！'}]}]" />
        </a-form-item>
        <a-form-item
          label="开发者私钥"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          has-feedback
        >
          <a-input placeholder="请输入开发者私钥" v-decorator="['privateKey', {rules: [{required: true, message: '请输入开发者私钥！'}]}]" />
        </a-form-item>
        <a-form-item
          label="异步回调地址"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          has-feedback
        >
          <a-input placeholder="请输入异步回调地址" v-decorator="['notifyUrl', {rules: [{required: true, message: '请输入异步回调地址！'}]}]" />
        </a-form-item>
      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>
  import { appPayWxWebConfigEdit } from '@/api/modular/app/pay/appPayWxWebConfigManage'
  export default {
    data () {
      return {
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 }
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 15 }
        },
        visible: false,
        confirmLoading: false,
        form: this.$form.createForm(this)
      }
    },
    methods: {
      // 初始化方法
      edit (record) {
        this.visible = true
        setTimeout(() => {
          this.form.setFieldsValue(
            {
              id: record.id,
              weight: record.weight,
              mchName: record.mchName,
              appId: record.appId,
              mchId: record.mchId,
              mchSerialNo: record.mchSerialNo,
              appKey: record.appKey,
              privateKey: record.privateKey,
              notifyUrl: record.notifyUrl,
              returnUrl: record.returnUrl
            }
          )
        }, 100)
      },
      handleSubmit () {
        const { form: { validateFields } } = this
        this.confirmLoading = true
        validateFields((errors, values) => {
          if (!errors) {
            for (const key in values) {
              if (typeof (values[key]) === 'object' && values[key] != null) {
                values[key] = JSON.stringify(values[key])
              }
            }
            appPayWxWebConfigEdit(values).then((res) => {
              if (res.success) {
                this.$message.success('编辑成功')
                this.confirmLoading = false
                this.$emit('ok', values)
                this.handleCancel()
              } else {
                this.$message.error('编辑失败')//  + res.message
              }
            }).finally((res) => {
              this.confirmLoading = false
            })
          } else {
            this.confirmLoading = false
          }
        })
      },
      handleCancel () {
        this.form.resetFields()
        this.visible = false
      }
    }
  }
</script>
